﻿Public Class xtf_ABMconfig_validacion 
    Private obj_config_validacion_ As cls_config_validacion_
    Private obj_Form As Form
    Private col_Controles As New Generic.List(Of Control)

    Private bit_NuevaConfig As Boolean = True

    Public Sub New(ByVal config_validacion_ As cls_config_validacion_)
        InitializeComponent()

        obj_config_validacion_ = config_validacion_
        obj_Form = CType(Activator.CreateInstance(Type.GetType(Replace(Application.ProductName, " ", "_") & "." & config_validacion_.nombreformulario, True, True)), Form)
        Me.Text = "Validación de Formulario: " & obj_Form.Text
        txt_NombreFormulario.Text = config_validacion_.tituloformulario

        CargarControles()
    End Sub

    Private Sub CargarControles()
        Dim i As Integer = 0


        For Each controlfor As DevExpress.XtraLayout.BaseLayoutItem In CType(obj_Form, Object).lac_general.Items
            If controlfor.TypeName = "LayoutControlItem" Then
                Dim control As DevExpress.XtraLayout.LayoutControlItem = controlfor
                Dim nombreControl As String = control.Control.Name
                If Strings.Left(controlfor.Name, 5) <> "NoVal" Then
                    If Strings.Left(nombreControl, 3) <> "lbl" And Strings.Left(nombreControl, 3) <> "tsp" Then 'No tiene en cuenta los tsp
                        Dim nombreCortoControl As String = Strings.Right(nombreControl, nombreControl.Length - 3)
                        Dim Elemento As New CustomListItem 'DevExpress.XtraEditors.Controls.  'New Infragistics.Win.UltraWinListView.UltraListViewItem

                        Elemento.text = Strings.Left(controlfor.Text, controlfor.Text.Length - 1)
                        If Elemento.text = "(*)" Then
                            Elemento.text = Strings.Left(controlfor.CustomizationFormText, controlfor.CustomizationFormText.Length - 1)
                        End If
                        Elemento.objeto = control.Control
                        'Elemento.Key = control.Name
                        'Elemento.Appearance.Image = Nothing


                        Try
                            If obj_config_validacion_.validaciones.Split(New Char() {";"}, StringSplitOptions.RemoveEmptyEntries)(i) = "1" Then
                                liv_Obligatorios.Items.Add(Elemento)
                            Else
                                liv_Opcionales.Items.Add(Elemento)
                            End If
                        Catch ex As Exception 'Primera vez o cambio en diseño
                            liv_Opcionales.Items.Add(Elemento)
                        End Try
                        col_Controles.Add(control.Control)

                        i += 1
                    End If
                End If
            End If

        Next
    End Sub

    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        GuardarCambios()
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub GuardarCambios()
        Dim str_Validaciones As String = ""
        Dim flag As Boolean = False
        For Each control As Control In col_Controles
            For Each item As CustomListItem In liv_Obligatorios.Items
                If control.Name = item.objeto.name Then
                    str_Validaciones &= "1;"
                    flag = True
                End If
            Next
            If flag = False Then
                str_Validaciones &= "0;"
            End If
            flag = False
        Next

        obj_config_validacion_.validaciones = str_Validaciones
        obj_config_validacion_.tituloformulario = txt_NombreFormulario.Text
        obj_config_validacion_.Modificar()
    End Sub

    Private Sub btn_Quitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Quitar.Click
        For i As Integer = 0 To liv_Opcionales.SelectedItems.Count - 1
            Dim Elemento As CustomListItem = liv_Opcionales.SelectedItems(0)
            liv_Opcionales.Items.Remove(Elemento)
            liv_Obligatorios.Items.Add(Elemento)
        Next
    End Sub

    Private Sub btn_Agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Agregar.Click
        For i As Integer = 0 To liv_Obligatorios.SelectedItems.Count - 1
            Dim Elemento As CustomListItem = liv_Obligatorios.SelectedItems(0)
            liv_Obligatorios.Items.Remove(Elemento)
            liv_Opcionales.Items.Add(Elemento)
        Next
    End Sub

    Private Sub btn_AgregarTodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_AgregarTodos.Click
        For i As Integer = 0 To liv_Obligatorios.Items.Count - 1
            Dim Elemento As CustomListItem = liv_Obligatorios.Items(0)
            liv_Obligatorios.Items.Remove(Elemento)
            liv_Opcionales.Items.Add(Elemento)
        Next
    End Sub

    Private Sub btn_QuitarTodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_QuitarTodos.Click
        For i As Integer = 0 To liv_Opcionales.Items.Count - 1
            Dim Elemento As CustomListItem = liv_Opcionales.Items(0)
            liv_Opcionales.Items.Remove(Elemento)
            liv_Obligatorios.Items.Add(Elemento)
        Next
    End Sub
End Class

